package kd.hc.dev.tpl;

import kd.bos.form.FormShowParameter;
import kd.bos.form.container.Container;
import kd.bos.form.control.Control;
import kd.bos.form.control.Toolbar;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.util.CollectionUtils;
import kd.sdk.plugin.Plugin;

import java.util.Collections;
import java.util.EventObject;
import java.util.List;


public class ListToolHideListPlugIn extends AbstractListPlugin implements Plugin {
    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        FormShowParameter formShowParameter = this.getView().getFormShowParameter();
        if (formShowParameter != null && formShowParameter.getCustomParams() != null && formShowParameter.getCustomParams().size() >= 1) {
            Boolean hideToolBarap = (Boolean) formShowParameter.getCustomParam("hideToolBarap");
            Boolean hideFilterContainerap = (Boolean) formShowParameter.getCustomParam("hideFilterContainerap");
            if (hideToolBarap != null && hideToolBarap) {
                this.getView().setVisible(false, "toolbarap");
            }
            if (hideFilterContainerap != null && hideFilterContainerap) {
                this.getView().setVisible(false, "filtercontainerap");
            }
        }
    }

    private void loopAndHideToolBar(List<Control> list) {
//        List<Control> controls = Collections.singletonList(this.getView().getRootControl());
//        loopAndHideToolBar(controls);
        if (CollectionUtils.isNotEmpty(list)) {
            for (Control control : list) {
                String key = control.getKey();
                if ("pagepanel".equals(key)) {
                    this.getView().setVisible(false, "pagepanel");
                } else if (control instanceof Toolbar) {
                    this.getView().setVisible(false, key);
                } else if (control instanceof Container) {
                    this.loopAndHideToolBar(((Container) control).getItems());
                }
            }

        }

    }


}